package com.xli.organ.user.frameuser.entity.mapper;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.xli.mis.codeitems.service.ICodeItemsService;
import com.xli.organ.ou.frameou.entity.FrameOu;
import com.xli.organ.ou.frameou.service.IFrameOuService;
import com.xli.organ.user.frameuser.entity.FrameUser;
import com.xli.organ.user.frameuser.entity.dto.UserAddDTO;
import com.xli.organ.user.frameuser.entity.dto.UserUpdateDTO;
import com.xli.organ.user.frameuser.entity.vo.UserVO;
import com.xli.security.sensitive.processor.SensitiveProcessor;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface IFrameUserMapper {

    IFrameUserMapper INSTANCE = Mappers.getMapper(IFrameUserMapper.class);

    IFrameOuService iFrameOuService = SpringUtil.getBean(IFrameOuService.class);
    ICodeItemsService iCodeItemsService = SpringUtil.getBean(ICodeItemsService.class);

    @Mappings({@Mapping(source = "loginId", target = "login_id"),
            @Mapping(source = "idNum", target = "id_num"),
            @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd"),
            @Mapping(source = "carNum", target = "car_num"),
            @Mapping(source = "ouId", target = "ou_id"),
            @Mapping(source = "orderNum", target = "order_num")})
    FrameUser toEntity(UserAddDTO dto);

    @Mappings({@Mapping(source = "idNum", target = "id_num"),
            @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd"),
            @Mapping(source = "carNum", target = "car_num"),
            @Mapping(source = "ouId", target = "ou_id"),
            @Mapping(source = "orderNum", target = "order_num")})
    FrameUser toEntity(UserUpdateDTO dto);

    @Mappings({@Mapping(source = "login_id", target = "loginId"),
            @Mapping(source = "id_num", target = "idNum"),
            @Mapping(source = "gender", target = "gender", qualifiedByName = "getGender"),
            @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd"),
            @Mapping(source = "car_num", target = "carNum"),
            @Mapping(source = "ou_id", target = "ouId"),
            @Mapping(source = "ou_id", target = "ouName", qualifiedByName = "findOuNameById"),
            @Mapping(source = "order_num", target = "orderNum")})
    UserVO toVO(FrameUser frameUser);

    @AfterMapping
    default void setDefaultValues(@MappingTarget FrameUser frameUser, Object dto) {
        if (dto instanceof UserAddDTO) {
            // 新增时才给默认值
            frameUser.setId(IdUtil.getSnowflakeNextId());
            frameUser.setIs_enabled("1");
            frameUser.setPwd("12345");
        }
    }

    @AfterMapping
    default void maskSensitiveFields(@MappingTarget UserVO userVO) {
        SensitiveProcessor.processSensitiveFields(userVO);
    }

    @Named("findOuNameById")
    default String findOuNameById(Long id) {
        if (!StrUtil.isBlankIfStr(id.toString())) {
            FrameOu frameOu = iFrameOuService.find(id);
            if (frameOu != null) {
                return frameOu.getOu_name();
            }
        }
        return "";
    }

    @Named("getGender")
    default String getGender(String gender) {
        return iCodeItemsService.findItemTextByValue("性别", gender);
    }
}
